package com.wen.common.filter;


import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 过滤器 ,交给spring容器就直接可以生效了
 * 因为整合了shiro ，有jwtFilter，所以在jwtFilter中报错要交给我们Filter捕获，并转发携带到Controller层
 * 所以在这，我们要在Filter的配置文件中把Filter交给spring容器
 *
 * （放里面测试发现正确的token也会报错，SessionContext must be an HTTP compatible implementation  需要日后解决！）
 */
@Slf4j
@Component
public class TestFilter implements Filter {

    private static final Logger logger = LoggerFactory.getLogger(TestFilter.class);

    @Autowired
    HttpServletRequest req;
    @Autowired
    HttpServletResponse resp;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("[ {} ] 创建啦...", this.getClass().getSimpleName());
        logger.info("resp: {}",resp);
        logger.info("req: {}",req);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        logger.info("[ {} ] 执行啦...", this.getClass().getSimpleName());

        //使过滤完往下走
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        logger.info("[ {} ] 被摧毁啦...", this.getClass().getSimpleName());
    }

}